﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web.DynamicData;

namespace DD_Extensions
{
    public partial class TextField : System.Web.DynamicData.FieldTemplateUserControl
    {
        private int maxDisplayLength = 25;

        public override string FieldValueString
        {
            get
            {
                // if column value
                var fieldOptions = MetadataAttributes.GetAttribute<FieldOptionsAttribute>();
                if (fieldOptions != null && fieldOptions.TruncateSize > 0)
                    maxDisplayLength = fieldOptions.TruncateSize;

                string value = base.FieldValueString.ToMaskedString(Column);
                if (ContainerType == ContainerType.List)
                {
                    if (value != null && value.Length > maxDisplayLength)
                        value = String.Format("<span title=\"{0}\">{1}&hellip;</span>", value, value.Substring(0, maxDisplayLength - 3));
                }
                return value;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // add CSS from meta data
            this.ApplyCss();
        }

        public override Control DataControl
        {
            get { return Literal1; }
        }
    }
}
